package com.cskaoyan.service.wx.impl;

import com.alibaba.druid.util.StringUtils;
import com.cskaoyan.bean.pojo.*;
import com.cskaoyan.bean.wx.foorprint.bo.WxFootDeleteBo;
import com.cskaoyan.bean.wx.foorprint.bo.WxFootprintListBo;
import com.cskaoyan.bean.wx.foorprint.vo.WxFootprintVo1;
import com.cskaoyan.bean.wx.foorprint.vo.WxFootprintVo;
import com.cskaoyan.mapper.FootprintMapper;
import com.cskaoyan.mapper.GoodsMapper;
import com.cskaoyan.mapper.UserMapper;
import com.cskaoyan.service.wx.WxFootprintService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @program: project-two
 * @description:
 * @author: YuHui
 * @create: 2021-08-15 17:31
 **/
@Transactional
@Service
public class WxFootprintServiceImpl implements WxFootprintService {

    @Autowired
    FootprintMapper footprintMapper;
    @Autowired
    GoodsMapper goodsMapper;

    @Autowired
    UserMapper userMapper;


    /**
     * 足迹刷新
     *
     * @param wxFootprintListBo
     * @return
     */
    @Override
    public WxFootprintVo list(WxFootprintListBo wxFootprintListBo) {

        PageHelper.startPage(wxFootprintListBo.getPage(), wxFootprintListBo.getSize());

        FootprintExample footprintExample = new FootprintExample();
        footprintExample.setOrderByClause("update_time desc" );
        FootprintExample.Criteria criteria = footprintExample.createCriteria();

        criteria.andUserIdEqualTo(getCurrentUserId());
        criteria.andDeletedEqualTo(false);

        List<Footprint> footprints = footprintMapper.selectByExample(footprintExample);
        WxFootprintVo wxFootprintVo = new WxFootprintVo();
        List<WxFootprintVo1> wxFootprintVo1List = new ArrayList<>();

        for (Footprint footprint : footprints) {

            Goods goods = goodsMapper.selectByPrimaryKey(footprint.getGoodsId());

            WxFootprintVo1 wxFootPrintVo1 = new WxFootprintVo1();
            wxFootPrintVo1.setName(goods.getName());
            wxFootPrintVo1.setBrief(goods.getBrief());
            wxFootPrintVo1.setPicUrl(goods.getPicUrl());
            wxFootPrintVo1.setAddTime(footprint.getAddTime());
            wxFootPrintVo1.setId(footprint.getId());
            wxFootPrintVo1.setRetailPrice(goods.getRetailPrice());
            wxFootprintVo1List.add(wxFootPrintVo1);
        }
        PageInfo<Footprint> footprintPageInfo = new PageInfo<>(footprints);
        int pages = footprintPageInfo.getPages();

        wxFootprintVo.setTotalPages(pages);
        wxFootprintVo.setFootprintList(wxFootprintVo1List);

        return wxFootprintVo;

    }

    /**
     * 足迹删除
     *
     * @param wxFootDeleteBo
     */
    @Override
    public void delete(WxFootDeleteBo wxFootDeleteBo) {

        Footprint footprint = new Footprint();
        footprint.setId(wxFootDeleteBo.getId());
        footprint.setDeleted(true);
        footprint.setUpdateTime(new Date());

        footprintMapper.updateByPrimaryKeySelective(footprint);

    }


    /**
     * 返回当前用户的用户id
     * @return
     */
    private Integer getCurrentUserId() {

        String username = (String) SecurityUtils.getSubject().getPrincipal();
        UserExample userExample = new UserExample();
        UserExample.Criteria criteria = userExample.createCriteria();
        if(!StringUtils.isEmpty(username)){
            criteria.andUsernameEqualTo(username);
        }
        criteria.andDeletedEqualTo(false);
        List<User> users = userMapper.selectByExample(userExample);
        return users.get(0).getId();
    }
}
